import { getRequest } from '@/utils/api'
import Vue from 'vue'
import Vuex from 'vuex'
import {Notification} from "element-ui"
Vue.use(Vuex)


const store = Vuex.Store({
    state:{
        routes:[],
        stomp:null,
        admins:[],
        currentSession: null,
        session:{},
        currentAdmin:JSON.parse(window.sessionStorage.getItem('user')),
        isDot:{}
    },
    mutations:{
        changecurrentsession(state,currentSession){
            state.currentSession = currentSession
              Vue.set(state.isDot,state.currentAdmin.username+"#"+state.currentSession.username.from,false)
        },
        INIT_ADMIN(state,admin){
            state.currentAdmin = admin
        },
        initRoutes(state,data){
            state.routes = data 
        },
        addMessage(state,msg){
            let mss = state.sessions[state.currentAdmin.username +"#"+msg.to]
              if(!mss) {
                let mss = state.sessions[state.currentAdmin.username+'#'+msg.to] =[]
                Vue.set(state.sessions,state.currentAdmin.username+'#'+msg.to,[ ])
            }
              state.sessions[state.currentAdmin.username+'#'+msg.to].push({
                  content:msg.content,
                  data:new Date(),
                  self:!msg.notSelf
              })
           
        },
        init_admins(state,data){
            state.admins = data
        },
       INIT_DATA(state){
           let data = localStorage.getItem('vue-chat-session')
           if(data){
               state.sessions = JSON.parse(data)
           }
       }
    },
    actions:{
        initData(context){
            getRequest('/chat/admin').then(resp=>{
                if(resp){
                    context.commit('init_admins',resp)
                }
            })
        },
        //消息的接受
        connect(context){
            context.state.stomp = Stomp.over(new SockJS('ws/ep'))
            let token = window.sessionStorage.getItem('tokenStr')
            context.state.stomp.connect({'Auth-Token':token},success=>{
                context.state.stomp.subscribe('/user/queue/chat',msg=>{
                    let receiveMsg = JSON.parse(msg.body)
                    if(!context.state.currentSession || receiveMsg.from != context.state.currentSession.username){
                        Notification.info({
                            title:'['+receiveMsg.fromNickName+']',
                            message: receiveMsg.connect.length>10?receiveMsg.content.substr(0,10):receiveMsg.content,
                            position:'bottom-right'
                        })
                      
                    }
                    receiveMsg.notSelf = true
                    receiveMsg.to = receiveMsg.from
                    context.commit('addMessage',receiveMsg)
                })
            },error=>{})
        }
    }
})

store.watch(function(state){
    return state.sessions
},function(val){
    localStorage.setItem('VUE-CHAT-SESSION',JSON.stringify(val))
},{
    deep:true
})
